/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cl.alanis.gui;

import cl.alanis.business.ProductoBusiness;
import cl.alanis.dto.ProductoDto;
import cl.alanis.util.AlanisLog;
import java.util.Iterator;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.apache.log4j.Logger;

/**
 *
 * @author Rosa
 */
public class BusquedaGui extends javax.swing.JDialog {
    Logger logger=AlanisLog.obtenerLogger(BusquedaGui.class);
    /**
     * Creates new form BusquedaGui
     */
    public BusquedaGui() {
        this.setModal(true);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        bttBusqueda2 = new javax.swing.JButton();
        cmbFiltroBusqueda2 = new javax.swing.JComboBox();
        txtBusqueda2 = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblListaBusqueda = new javax.swing.JTable();
        tblListaBusqueda1 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        lblError = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("- BÚSQUEDA DE PRODUCTOS / INFORME DE STOCK -");

        bttBusqueda2.setText("Buscar");
        bttBusqueda2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttBusqueda2ActionPerformed(evt);
            }
        });

        cmbFiltroBusqueda2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "Código Producto", "Descripción", "Aplicación", "Marca Auto", "Año Auto", "Fabricante" }));

        jLabel16.setText("Filtro de Búsqueda");

        tblListaBusqueda.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código Producto", "Descripción", "Aplicación", "Stock", "Valor s/Iva", "Marca Auto", "Año Auto", "Fabricante (R)", "Tipo Repuesto"
            }
        ));
        jScrollPane1.setViewportView(tblListaBusqueda);

        tblListaBusqueda1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código Producto", "Descripción", "Aplicación", "Marca Auto", "Año Auto", "Modelo Auto", "Fabricante (R)", "Tipo Repuesto"
            }
        ));

        jLabel2.setText("RESULTADOS");

        lblError.setForeground(new java.awt.Color(255, 0, 0));
        lblError.setText("-");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(312, 312, 312)
                        .addComponent(jLabel1)
                        .addGap(0, 211, Short.MAX_VALUE)))
                .addGap(26, 26, 26))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbFiltroBusqueda2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel16))
                        .addGap(59, 59, 59)
                        .addComponent(txtBusqueda2, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(bttBusqueda2))
                    .addComponent(jLabel2)
                    .addComponent(lblError))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 45, Short.MAX_VALUE)
                    .addComponent(tblListaBusqueda1, javax.swing.GroupLayout.PREFERRED_SIZE, 717, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 44, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(5, 5, 5)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel16)
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbFiltroBusqueda2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtBusqueda2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bttBusqueda2))
                .addGap(28, 28, 28)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblError)
                .addContainerGap(26, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 192, Short.MAX_VALUE)
                    .addComponent(tblListaBusqueda1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 192, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bttBusqueda2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttBusqueda2ActionPerformed
        
        DefaultTableModel model = (DefaultTableModel) tblListaBusqueda.getModel();
        while (model.getRowCount()>0){
            model.removeRow(0);
        }
        
        String texto=txtBusqueda2.getText();
        ProductoDto producto=new ProductoDto();
        ProductoBusiness business = new ProductoBusiness();
        switch (cmbFiltroBusqueda2.getSelectedIndex()){
            case 0:producto.setFiltroGeneral(texto); break;
            case 1:producto.setCodProducto(texto); break;
            case 2:producto.setDescripcionProducto(texto); break;
            case 3:producto.setAplicacion(texto); break;
            case 4:producto.setMarca(texto); break;
            case 5:producto.setAgnoAuto(texto); break;
            case 6:producto.setFabricante(texto); break;
            case 7:producto.setOrigenRepuesto(texto); break;
        }
        try {
            List lista=business.buscarProductoFiltro2(producto);
            Iterator iterador=lista.iterator();
            while (iterador.hasNext()){
                producto=(ProductoDto)iterador.next();
                Object[] fila=business.obtenerFilaTablaBuscaProd2(producto);
                model.addRow(fila);
                
            }
        } catch (Exception ex) {
            logger.error("Error:"+ex.getMessage());
            lblError.setText(ex.getMessage());
        }
            
    }//GEN-LAST:event_bttBusqueda2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BusquedaGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BusquedaGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BusquedaGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BusquedaGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new BusquedaGui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bttBusqueda2;
    private javax.swing.JComboBox cmbFiltroBusqueda2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblError;
    private javax.swing.JTable tblListaBusqueda;
    private javax.swing.JTable tblListaBusqueda1;
    private javax.swing.JTextField txtBusqueda2;
    // End of variables declaration//GEN-END:variables
}
